<?php 

Class wechatController extends appController
{
	
	
	public function weixin()
	{
		
		$wx= new weixinController();
		$data=$wx->data;
		
		
		/*验证*/
		if($_GET["echostr"])
		{
			$respon=$wx->valid(Model('wechat')->getToken());
			//$this->errorlog($respon,ERROR_PATH);
			if($respon!='')
			{
				//Model('wechat')->update_vaild();
				die($respon);
			}
		}

		
		$app=Model('wechat')->get_app();
		$wx->getToken($app['appid'], $app['appsecret']);
		//关键词回复
		
if($data['Content'])
{  
	
	//自定义关键词回复
	
	$content=Model('wechat')->key_reply($data['Content']);
	
		
	 if(!empty($content))
	 {   
	   if(!is_array($content))
	   {   
	    $wx->replyTextMsg($content);
	   }
	   else
	   {
		
		
		$wx->replyImgMsg($content);
	   }
	 }
	 else 
	 {
	 	$areply=Model('wechat')->get_dreply();
	 	if(!is_array($areply))
	 	{
	 		$wx->replyTextMsg($areply);
	 	}
	 	else
	 	{
	 	
	 		$wx->replyImgMsg($areply);
	 	}
	 }
	
}
		//自定义菜单回复
		if($data['EventKey'])
		{
			
			if(strstr($data['EventKey'],'event_'))
			{
				$pos=strpos($data['EventKey'],'event_');
				$key= intval(substr($data['EventKey'],$pos+6));
				$result=Model('wechat')->get_msg($key);
				
				
				if(!is_array($result))
				{
						
					$wx->sendTextMsg($data['FromUserName'],$result);
				}
				else 
				{
					
					if(!empty($result))
					{
					   foreach($result as $k=>$v)
					  {
						$img_data[$k]['title']=urlencode($v['title']);
						$img_data[$k]['description']=urlencode($v['img_des']);
						$img_data[$k]['url']=$v['url'];
						$img_data[$k]['picurl']= $v['pic'];
					   }
						
					$wx->sendImgMsg($data['FromUserName'],$img_data);
					}
				}		
			}
		}
		/*关注时回复*/
		if($data['Event']=='subscribe')
		{
			
			$areply=Model('wechat')->get_areply();
			if(!is_array($areply))
			{
				$wx->replyTextMsg($areply);
			}
			else
			{
				
				$wx->replyImgMsg($areply);
			}
			
			//$areply=Model('wechat')->save_user($data['FromUserName']);
		}
		elseif($data['Event']=='unsubscribe')
		{
			//$areply=Model('wechat')->cancel_follow($data['FromUserName']);
			
		}
		/*发送地址*/
		
		if($data['Location_X']&&$data['Location_Y'])
		{
			$img_one=array("url"=>"http://".$_SERVER['HTTP_HOST']."/index.php?g=home&m=baidu&a=index&x=".$data['Location_X']."&y=".$data['Location_Y'],"thumb"=>"Home/webroot/default/images/i96x66.jpg","title"=>"分店信息","description"=>"分店信息");
			$img_arr[]=$img_one;
			$wx->replyImgMsg($img_arr);
		}
	}
	
	public function index(){
		
	}
}